// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/questionnaire/api/IbQuestionnaireApi'

import ListClass from '@api/questionnaire/model/QuestionnaireDetailReq'

const CLIENT = CORE.grpcCLIENT(Client)

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  validParam(param) {
    return param.questionnaireId
  },
  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param)
    const { comQuery = {}, dtos = [] } = await CORE.grpc(TIP, CLIENT, 'questionnaireStatistics', DTO)
    return [comQuery.totalCount || dtos.length, dtos]
  },
  transList(arr, { rootState: { one: { questionList }, answer: { total } } }) {
    return questionList.map(({ id, optionList: options, questionList: subs, ...other }) => {
      // 转换评分统计
      const transRate = (questionId) => {
        const { wordCloudDtoList = [] } = arr.find((item) => item.questionId === questionId) || {}
        const rateList = Array(other.max).fill().map((_, i) => {
          const star = `${i + 1}`
          const tip = other[`tip_${star}`]
          const num = (wordCloudDtoList.find(({ word }) => word === star) || {}).num || 0
          const percent = +(num * 100 / (total || 1)).toFixed(2)
          return { star, tip, num, percent }
        })
        const xData = rateList.map((item) => item.star)
        const dataList = rateList.map((item) => item.num)
        return { rateList, numChart: { xData, dataList } }
      }

      // 转换词语统计
      const transWord = (questionId) => {
        const { wordCloudDtoList = [{ word: '', num: 0 }] } = arr.find((item) => item.questionId === questionId) || {}
        const xData = wordCloudDtoList.map((item) => item.word)
        const dataList = wordCloudDtoList.map((item) => item.num)
        return { numChart: { xData, dataList } }
      }

      // 转换选项统计
      const transOption = (questionId) => {
        const { answerList = [] } = arr.find((item) => item.questionId === questionId) || {}
        const optionList = options.map((option) => {
          const { num } = answerList.find(({ optionId }) => optionId === option.id) || { num: 0 }
          const percent = +(num * 100 / (total || 1)).toFixed(2)
          return { ...option, num, percent }
        })
        const xData = optionList.map((option) => option.label)
        const numList = optionList.map((option) => option.num)
        const percentList = optionList.map((option) => option.percent)
        return { optionList, numChart: { xData, dataList: numList }, percentChart: { xData, dataList: percentList } }
      }

      // 评分题
      if (other.type === 'rate') {
        return { ...other, id, ...transRate(id) }
      }

      // 无子问题的
      if (CORE.isEmpty(subs)) {
        if (CORE.isEmpty(options)) { // 无选项的 (问答题、评分题)
          return { ...other, id, ...transWord(id) }
        } else { // 有选项的 (单选题、多选题)
          return { ...other, id, ...transOption(id) }
        }
      }

      // 有子问题的
      const subList = subs.map((sub) => {
        if (sub.type === 'textarea') { // 表格问答
          const subSubList = sub.questionList.map((subSub) => ({ ...subSub, ...transWord(subSub.id) }))
          return { ...sub, questionList: subSubList }
        } else { // 表格单选、表格多选
          return { ...sub, ...transOption(sub.id) }
        }
      })
      return { ...other, id, optionList: options, questionList: subList }
    })
  },
}
